//
//  SZZUserAccountViewModel.swift
//  SZZweibo
//
//  Created by 申壮壮 on 16/8/13.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class SZZUserAccountViewModel: NSObject {

    static let sharedTools:SZZUserAccountViewModel = SZZUserAccountViewModel()
    // 用户信息对象(为了防止频繁的操作沙盒)
    var userAccount: SZZUserAcconuntModel?
    // 路径
    let file = (NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("userAccount.archiver")
    var accessToken: String?{
        if userAccount?.access_token != nil {
            if userAccount?.expires_Date?.compare(NSDate()) == NSComparisonResult.OrderedDescending {
                return userAccount?.access_token
            }else{
                return nil
            }
            
        }else {
            // 代表用户没有登录过
            return nil
        }
    }
    
    var isLogin: Bool {
        return accessToken != nil
    }
    
    override init() {
        super.init()
        userAccount = getUserAccount()
    }
}
extension SZZUserAccountViewModel {
    // 保存对象
    func saveUserAccount(userAccount: SZZUserAcconuntModel){
        
        self.userAccount = userAccount
        // 保存对象
        NSKeyedArchiver.archiveRootObject(userAccount, toFile: file)
    }
    
    // 获得对象
    func getUserAccount() -> SZZUserAcconuntModel?{
        // 获取对象
        let result = NSKeyedUnarchiver.unarchiveObjectWithFile(file) as? SZZUserAcconuntModel
        return result
    }
}
// 请求数据
extension SZZUserAccountViewModel {
    // 获取授权
    func getUserAccount(code: String, finish:(isSuccess: Bool)->()){
        
        SZZNetworkTools.sharedTools.oauthLoadUserAccount(code, success: { (response) -> () in
            
            guard let res = response as? [String: AnyObject] else{
                finish(isSuccess: false)
                return
            }
            // 通过字典创建一个模型
            let userAccount = SZZUserAcconuntModel(dict: res)
            print(userAccount)
            self.getUserInfo(userAccount, finish: finish)
        }) { (error) -> () in
            print("请求失败:\(error)")
            finish(isSuccess: false)
        }
        
    }
    
    // 请求个人信息
    func getUserInfo(userAccount: SZZUserAcconuntModel, finish:(isSuccess: Bool)->()){
        
        SZZNetworkTools.sharedTools.oauthLoadUserInfo(userAccount, success: { (response) -> () in
            // 判断是否为nil 是否可以转成一个字典
            guard let res = response as?[String:AnyObject] else{
                finish(isSuccess: false)
                return
            }
            // 赋值
            userAccount.screen_name = res["screen_name"] as? String
            userAccount.avatar_large = res["avatar_large"] as? String
            
            print(userAccount)
            // 保存对象
            self.saveUserAccount(userAccount)
            finish(isSuccess: true)
        }) { (error) -> () in
            print("请求失败:\(error)")
            finish(isSuccess: false)
        }
    }
    
}

